package com.hfmall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfmall.entity.HfOrder;
import com.hfmall.entity.HfOrderCount;
import com.hfmall.entity.PageBean;
import com.hfmall.mapper.HfOrderMapper;
import com.hfmall.service.IHfOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liclo
 * @since 2021-08-12
 */
@Service
public class HfOrderServiceImpl extends ServiceImpl<HfOrderMapper, HfOrder> implements IHfOrderService {

    @Autowired
    private HfOrderMapper orderMapper;

    @Override
    public HfOrderCount getCount(int userId) {

        HfOrderCount count = new HfOrderCount();
       count.setAll(orderMapper.getAll(userId));
       count.setWaitPay(orderMapper.getCountByStatus(userId,"1"));
        count.setWaitSend(orderMapper.getCountByStatus(userId,"2"));
        count.setWaitReceive(orderMapper.getCountByStatus(userId,"3"));
        count.setWaitWrite(orderMapper.getCountByStatus(userId,"4"));

        return count;
    }

    @Override
    public List<HfOrder> list(HfOrder order) {


        return  orderMapper.list( order);
    }

    @Override
    public  PageInfo<HfOrder> getPages(Integer current, int userId, String statusNo, Integer size) {

        HfOrder order = new HfOrder();
        order.setUserId(userId);
        order.setStatus(statusNo);


        PageHelper.startPage(current,size);
        List<HfOrder> orders = orderMapper.list(order);
        //创建pageInfo对象
        PageInfo<HfOrder> pageInfo = new PageInfo<>(orders);

//
//        //获取总记录数
//        int totalCount = (int) pageInfo.getTotal();
//        //计算总页数
//        int totalPage = totalCount%pageSize == 0 ? totalCount/pageSize:totalCount/pageSize+1;
//
//        PageBean<HfOrder> pb = new PageBean<HfOrder>();
//        //设置当前页面数据集合
//        pb.setList(pageInfo.getList());
//        //设置当前页数
//        pb.setCurrentPage(page);
//        //设置页面大小
//        pb.setPageSize(pageSize);
//        //设置总记录数
//        pb.setTotalCount(totalCount);
//        //设置总页数
//        pb.setTotalPage(totalPage);
//        //设置当前页数量
//        //pb.setCurrentPageCount(pb.getList().size());


        return pageInfo;
    }

    @Override
    public HfOrder getOne(int id) {

        return orderMapper.getOne(id);
    }

    @Override
    public List<HfOrder> getByCondition(HfOrder order) {
        return orderMapper.getByCondition(order);
    }

    @Override
    public void updateByID(HfOrder order) {
        orderMapper.updateByID(order);
    }

    @Override
    public List<HfOrder> selectByTime(String startTime, String endTime) {
       return orderMapper.selectByTime(startTime,endTime);
    }

    @Override
    public List<HfOrder> selectByStatus(int status) {
        return orderMapper.selectByStatus(status);
    }

    @Override
    public PageBean<HfOrder> getB(int currPage, HfOrder order) {
            int pageSize=4;
            PageHelper.startPage(currPage, pageSize);
            List<HfOrder> orders=orderMapper.getByCondition(order);
            PageInfo<HfOrder> pageInfo=new PageInfo<>(orders);
            PageBean<HfOrder> pageBean=new PageBean<>();
            pageBean.setList(pageInfo.getList());
            pageBean.setPageSize(pageSize);
            pageBean.setCurrentPage(currPage);
            pageBean.setCurrentPageCount(pageInfo.getList().size());
            int totalNums=(int)pageInfo.getTotal();
            int totalPage=totalNums%pageSize==0? totalNums/pageSize:totalNums/pageSize+1;
            pageBean.setTotalPage(totalPage);
            pageBean.setTotalCount(totalNums);
            return pageBean;
    }

}
